package org.example.judge_comment.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.judge_comment.service.CommentLikeService;
import org.example.judge_common.annotation.AuthAnnotation;
import org.example.judge_common.entity.ResponseResult;
import org.example.judge_pojo.Auth;
import org.example.judge_pojo.CommentLike;
import org.example.judge_pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/comment/like")
public class CommentLikeController {

    @Qualifier("CommentLikeServiceImpl")
    @Autowired
    private CommentLikeService commentLikeService;

    /**
     * 判断/获取用户是否点赞
     * @param commentId 评论/帖子
     * @param request get=>loginUser.id
     * @return
     */
    @PostMapping("/byComment/{commentId}")
    @AuthAnnotation({Auth.CLIENT})
    public ResponseResult getByComment(@PathVariable String commentId, HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");
        CommentLike like = commentLikeService.isLike(commentId, loginUser.getId());
        return ResponseResult.success().setData(like);
    }

    /**
     * 用户点赞/取消
     * @param commentId
     * @param request
     * @return
     */
    @PutMapping("/byComment/{commentId}")
    @AuthAnnotation({Auth.CLIENT})
    public ResponseResult clickByComment(@PathVariable String commentId, HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");
//        CommentLike like = commentLikeService.isLike(commentId, loginUser.getId());
        CommentLike like = commentLikeService.clickLike(commentId, loginUser.getId());
        return ResponseResult.success().setData(like);
    }
}
